#!/bin/sh
#########################################################################################
# (C) Copyright NetEnrich Inc., 2008, All Rights Reserved.                              #
# File Name                     : cluster_kick_start.sh                                 #
# Purpose                       : To install the requried packages in the kick start    #
#                               : process with handling reboots                         #
#                               :                                                       #
#                               :                                                       #
# Version                       :                                                       #
# Revision                      :                                                       #
# Original Author               : Sunil Varma                                           #
# Date                          : Fri June 27 IST 2008									#
# Modification details          :                                                       #
# Reviewed By                   :                                                       #
# tested by                     :                                                       #
#########################################################################################

LOGFILE=/post-install.log
DATA_TAR_FILE=centos-x86_64.tar.gz
DATA_PATH=/tmp/centos
MODEL=$1

#
# yum related changes
#
UpdateYumConf () {
	rm -rf /etc/yum.repos.d/*
	if [ $? == 0 ]; then
		echo "SUCCESS: Updated yum" >> $LOGFILE
	else
		echo "FAILURE: Updated yum" >> $LOGFILE
		exit 1;
	fi
	yum clean all
}

#
# Instaslling packages
#
InstallPackages () {
	MODEL=$1;
	
	echo "-----> UI::Dialog was not added <----- " >>$LOGFILE;
	## Installing the UI::Dialog compiled
	#tar -zxf $DATA_PATH/packages/perl/UI-Dialog-1.08-compiled.tar.gz -C /tmp
	#cd /tmp/UI-Dialog-1.08
    ##cd $DATA_PATH/packages/perl/UI-Dialog-1.08 
	#make install 
    #if [ $? == 0 ]; then
    #rm -rf /tmp/UI-Dialog-1.08
    #    echo "SUCCESS: install perl UI::Dialog" >>$LOGFILE;
    #else
    #    echo "FAILURE: install perl UI::Dialog" >>$LOGFILE;
    #    exit 1;
    #fi

	echo "packages/perl/perl-Time-HiRes-smp-1.9715.tar.gz is replaced with perl-Time-HiRes-1.9725-3.el7.x86_64.rpm ... " >>$LOGFILE;
	
	#Installing SNMP
	SNMP_RETURN=''
	tar -xvzpPf $DATA_PATH/packages/snmp/snmp_standalone_centos.tgz
	SNMP_RETURN=$?
	if [ $SNMP_RETURN == 0 ]; then
	    echo "SUCCESS: install standalone snmp" >>$LOGFILE;
	else
	    echo "FAILURE: install standalone snmp" >>$LOGFILE;
	    exit 1;
	fi
}

#
# User and group creation 
# 
CreateUser () {
	useradd nem -u 499 -d /home/nem 2>&1
	STATE1=$?
	useradd nems -u 501 -d /nems 2>&1
	STATE2=$?
	useradd admin -u 502 -s /usr/local/nems/bin/menu 2>&1
	STATE3=$?
	useradd nemsdb -u 503 -d /home/nemsdb 2>&1
	STATE4=$?

	useradd ppp -u 504 -s /usr/sbin/pppd 2>&1
	STATE5=$?
	chmod 4755 /usr/sbin/pppd
	STATE6=$?
	
	#Adding the nemdb group
	groupadd -f nemsdb
	STATE7=$?

	# Modification of the group
	sed -e "s/nems:x:501:/nems:x:501:nems,admin/g" < /etc/group > /etc/group.new && cp /etc/group.new /etc/group && rm /etc/group.new
	STATE8=$?
	sed -e "s/admin:x:502:/admin:x:502:admin,nems/g" < /etc/group > /etc/group.new && cp /etc/group.new /etc/group && rm /etc/group.new
	STATE9=$?

	useradd neadmin -u 505 2>&1
	STATE10=$?

	if [ $STATE1 == 0 -a $STATE2 == 0 -a $STATE3 == 0 -a $STATE4 == 0  -a $STATE5 == 0  -a $STATE6 == 0  -a $STATE7 == 0  -a $STATE8 == 0  -a $STATE9 == 0 -a $STATE10 == 0 ]; then
		echo "SUCCESS: User and group creation" >> $LOGFILE
	else 
		echo "FAILURE: User and group creation" >> $LOGFILE
		exit 1;
	fi
}

#
# Scripts for the hardening of the box 
#
HardenVPMG () {
	cd $DATA_PATH/hardening
	tar -xvzf hard_final.tar.gz >/dev/null 2>&1
	STATE1=$?
	chmod a+rx auto-harden.sh
	STATE2=$?
	./auto-harden.sh >/dev/null 2>&1
	STATE3=$?
	if [ $STATE1 == 0 -a $STATE2 == 0 -a $STATE3 == 0 ]; then
        	echo "SUCCESS: Completed hardening process." >> $LOGFILE
	else
        	echo "FAILURE: Failed at hardening.  Exiting..." >> $LOGFILE
        	exit 1;
	fi
}

#
# prevent kudzu from scanning for ports during bootup
#
DisableKudzu () {
	sed "s:SAFE=no:SAFE=yes:g" < /etc/sysconfig/kudzu > /tmp/kudzu.tmp
	STATE1=$?
	mv /tmp/kudzu.tmp /etc/sysconfig/kudzu
	STATE2=$?

	if [ $STATE1 == 0 -a $STATE2 == 0 ]; then
		echo "SUCCESS: prevent kudzu from scanning for ports" >> $LOGFILE
	else 
		echo "FAILURE: prevent kudzu from scanning for ports" >> $LOGFILE
		exit 1;
	fi
}

#
# Set Default Factory passwords
#
SetDefaultPass () {
	#echo 'xAVa6Aze' | passwd --stdin root
	#passwd -l root
	echo 'vistara' | passwd --stdin root
	STATE1=$?
	echo "admin" | passwd --stdin admin
	STATE2=$?
	echo "N3mR0ck5" | passwd --stdin nem
	STATE3=$?
	echo "ppppp" | passwd --stdin ppp
	STATE4=$?
	passwd -x -1 nems
	STATE5=$?
	passwd -x -1 admin
	STATE6=$?
	passwd -x -1 nemsdb
	STATE7=$?
	passwd -x -1 ppp
	STATE8=$?
	passwd -x -1 root
	STATE9=$?
	passwd -x -1 nem
	STATE10=$?
	echo "umask 0007" >> /nems/.bash_profile
	STATE11=$?
	echo "xAVa6Aze" | passwd --stdin neadmin
	STATE12=$?
	passwd -x -1 neadmin
	STATE13=$?

	if [ $STATE1 == 0 -a $STATE2 == 0 -a $STATE3 == 0 -a $STATE4 == 0  -a $STATE5 == 0  -a $STATE6 == 0  -a $STATE7 == 0  -a $STATE8 == 0  -a $STATE9 == 0  -a $STATE10 == 0  -a $STATE11 == 0 -a $STATE12 == 0 -a $STATE13 == 0 ]; then
		echo "SUCCESS: Set Default Factory passwords" >> $LOGFILE
	else 
		echo "FAILURE: Set Default Factory passwords" >> $LOGFILE
		exit 1;
	fi
}

#
# Copying keys for gnupg 
# 
CopyKeys () {
	/bin/mkdir /home/admin/.gnupg
	STATE1=$?
	/bin/cp $DATA_PATH/keys/*.gpg /home/admin/.gnupg
	STATE2=$?
	/bin/cp $DATA_PATH/keys/gpg.conf /home/admin/.gnupg
	STATE3=$?
	/bin/chmod 700 /home/admin/.gnupg
	STATE4=$?
	/bin/chown -R admin /home/admin/.gnupg
	STATE5=$?

	if [ $STATE1 == 0 -a $STATE2 == 0 -a $STATE3 == 0 -a $STATE4 == 0  -a $STATE5 == 0 ]; then
		echo "SUCCESS: keys for gnupg " >> $LOGFILE
	else 
		echo "FAILURE: keys for gnupg " >> $LOGFILE
		exit 1;
	fi
}

#
# copying conf files 
#
CopyConfFiles () {
	
	MODEL=$1;
	# copy syslog
	/bin/cp $DATA_PATH/conf-files/syslog/init.d/syslog /etc/rc.d/init.d/
	STATE1=$?
	/bin/chmod 755 /etc/rc.d/init.d/syslog
	STATE2=$?
	/bin/cp $DATA_PATH/conf-files/syslog/syslog /etc/sysconfig/
	STATE3=$?
	/bin/chmod 644 /etc/sysconfig/syslog
	STATE4=$?
	
	/bin/cp $DATA_PATH/conf-files/services/services /etc/services
	STATE5=$?
	/bin/chmod 644 /etc/services
	STATE6=$?
	
	# copy sshd
	/bin/cp $DATA_PATH/conf-files/sshd/sshd_config /etc/ssh/sshd_config
	STATE7=$?
	/bin/chmod 600 /etc/ssh/sshd_config
	STATE8=$?
	
	# copy system-auth
	cp $DATA_PATH/conf-files/system-auth/system-auth /etc/pam.d/system-auth
	STATE9=$?
	chmod 644 /etc/pam.d/system-auth
	STATE10=$?

	# copy fstab
	cp -f $DATA_PATH/conf-files/fstab/$MODEL/fstab /etc
	STATE11=$?

	# copy ppp files
	cp -f $DATA_PATH/conf-files/ppp/options.ttyUSB0 /etc/ppp/
	STATE12=$?
	cp -f $DATA_PATH/conf-files/ppp/options /etc/ppp/
	STATE13=$?
	echo "-----> Ignored 'cp -f conf-files/ppp/login.config /etc/mgetty+sendfax/' as target directory does not exists <-----" >> $LOGFILE
	#cp -f $DATA_PATH/conf-files/ppp/login.config /etc/mgetty+sendfax/	
	STATE14=$?

	if [ $STATE1 == 0 -a $STATE2 == 0 -a $STATE3 == 0 -a $STATE4 == 0  -a $STATE5 == 0  -a $STATE6 == 0  -a $STATE7 == 0  -a $STATE8 == 0  -a $STATE9 == 0  -a $STATE10 == 0  -a $STATE11 == 0 -a $STATE12 == 0 -a $STATE13 == 0 -a $STATE14 == 0 ]; then
		echo "SUCCESS: copying conf files" >> $LOGFILE
	else 
		echo "FAILURE: copying conf files" >> $LOGFILE
		exit 1;
	fi
}

#
# Creating the faillog
#
CreateFailLog () {
	touch /var/log/faillog
	# the maximum fail limit for ppp and no limity for root and admin
	/usr/bin/faillog -u ppp -m 3
	STATE1=$?
	/usr/bin/faillog -u admin -m -1
	STATE2=$?
	/usr/bin/faillog -u root -m -1
	STATE3=$?

	if [ $STATE1 == 0 -a $STATE2 == 0 -a $STATE3 == 0 ]; then
		echo "SUCCESS: creating faillog" >> $LOGFILE
	else 
		echo "FAILURE: creating faillo" >> $LOGFILE
		exit 1;
	fi
}

#
# Enabling swap
#
EnableSwap () {

	MODEL=$1
	STATE1=''
	mkswap /dev/sda3
	STATE1=$?
	swapon -a -e
	STATE2=$?
	if [ $STATE1 == 0 -a $STATE2 == 0 ]; then
		echo "SUCCESS: enabling swap" >> $LOGFILE
	else 
		echo "FAILURE: enabling swap" >> $LOGFILE
		exit 1;
	fi
}

#
# Increasing ulimit
# 
ModUlimit () {
	sed -e "/* soft core 0/d" -e "/* hard core 0/d"< /etc/security/limits.conf > /tmp/limits.conf
	STATE1=$?
	rm -rf /etc/security/limits.conf  > /dev/null 2>&1
	STATE2=$?
	echo "* soft nofile 4096" >> /tmp/limits.conf
	STATE3=$?
	echo "* hard nofile 4096" >> /tmp/limits.conf
	STATE4=$?
	cat /tmp/limits.conf >> /etc/security/limits.conf
	STATE5=$?
	rm -rf /tmp/limits.conf > /dev/null 2>&1

	if [ $STATE1 == 0 -a $STATE2 == 0 -a $STATE3 == 0 -a $STATE4 == 0  -a $STATE5 == 0 ]; then
		echo "SUCCESS: Increase ulimit" >> $LOGFILE
	else 
		echo "FAILURE: Increase ulimit" >> $LOGFILE
		exit 1;
	fi
}


#
# Install VPMG firmware
#
VPMGBuidInstall () {
	mkdir /tmp/firmware
	cd /tmp/firmware
	tar -xvzf /firmware/firmware.tar.gz  > /dev/null 2>&1
	STATE1=$?
	
	echo "Installing VPMG" >> $LOGFILE
	perl install.pl install
	STATE2=$?
	if [ $STATE1 == 0 -a $STATE2 == 0 ]; then
		echo "SUCCESS: Installing VPMG" >> $LOGFILE
	else 
		echo "FAILURE: Installing VPMG" >> $LOGFILE
		exit 1;
	fi
}

#
# Update KS version
#
UpdateKSVersion () {
	
	MODEL=$1;
	/bin/cp $DATA_PATH/conf-files/ks-info/$MODEL/kickstart-info /root/
	STATE1=$?
	/bin/cp $DATA_PATH/conf-files/ks-info/$MODEL/kickstart-info /home/admin/logs/
	STATE2=$?
	chown admin.admin /home/admin/logs/kickstart-info
	STATE3=$?
	chmod 755 /home/admin/logs/kickstart-info
	STATE4=$?
	cat /home/admin/logs/kickstart-info | grep "Version=" -m1 > /home/admin/data/ksinfo
	STATE5=$?
	chown admin.admin /home/admin/data/ksinfo
	STATE6=$?
	chmod 770 /home/admin/data/ksinfo
	STATE7=$?

	if [ $STATE1 == 0 -a $STATE2 == 0 -a $STATE3 == 0 -a $STATE4 == 0  -a $STATE5 == 0  -a $STATE6 == 0  -a $STATE7 == 0 ]; then
		echo "SUCCESS: Updating KS version" >> $LOGFILE
	else 
		echo "FAILURE: Updating KS version" >> $LOGFILE
		exit 1;
	fi
}

#
# Copying controllers firmware 
#
CopyControllerFirmware () {
	mkdir -p /nems/data/tftphome/controller
	mkdir -p /nems/data/tftphome/controller/versions
	mkdir -p /nems/data/tftphome/controller/defaults
	mkdir -p /nems/data/tftphome/controller/defaults/4.1

	cp -r $DATA_PATH/controller/controller-firmware/* /nems/data/tftphome/controller/versions
	STATE1=$?
	cp -f $DATA_PATH/controller/controller-binaries/tftphome/vpmhcfirmware /nems/data/tftphome/
	STATE2=$?
	cp -f $DATA_PATH/controller/controller-binaries/vpmhcfirmware /nems/data/tftphome/controller/defaults/4.1
	STATE3=$?
	cp -f $DATA_PATH/controller/controller-binaries/u-boot.bin /nems/data/tftphome/controller/defaults/4.1
	STATE4=$?
	cp -f $DATA_PATH/controller/controller-binaries/runImg_Emrg /nems/data/tftphome/controller/defaults/4.1
	STATE5=$?

	chmod 774 /nems/data/tftphome/vpmhcfirmware
	STATE6=$?
	chmod -R 774 /nems/data/tftphome/controller/
	STATE7=$?
	chown nems.admin /nems/data/tftphome/vpmhcfirmware
	STATE8=$?
	chown -R nems.admin /nems/data/tftphome/controller/
	STATE9=$?

	rm -rf /nems/data/tftphome/controller/vpmhcfirmware

	if [ $STATE1 == 0 -a $STATE2 == 0 -a $STATE3 == 0 -a $STATE4 == 0  -a $STATE5 == 0  -a $STATE6 == 0  -a $STATE7 == 0 -a $STATE8 == 0  -a $STATE9 == 0 ]; then
		echo "SUCCESS: Updating KS version" >> $LOGFILE
	else 
		echo "FAILURE: Updating KS version" >> $LOGFILE
		exit 1;
	fi

}

#
#Creating the directory for the data recovery
#
VPMGRecovery () {
	mkdir -p /nems/data/recovery
	chown -R nems.nems /nems/data/recovery
	chmod 777 /nems/data/recovery
}


#
# Optimising the services 
#
OptimizeServices () {

	MODEL=$1;
	systemctl enable tftpd
	STATE1=$?
	systemctl enable snmpd
	STATE2=$?
	systemctl enable nfs
	STATE3=$?
	systemctl enable portmap
	STATE4=$?
	systemctl enable tftpd
	STATE5=$?
	systemctl enable iptables
	STATE6=$?
	systemctl enable netfs
	STATE7=$?

	if [ $STATE1 == 0 -a $STATE2 == 0 -a $STATE3 == 0 -a $STATE4 == 0  -a $STATE5 == 0  -a $STATE6 == 0  -a $STATE7 == 0 ]; then
		echo "SUCCESS: Adding services" >> $LOGFILE
	else 
		echo "FAILURE: Adding services" >> $LOGFILE
		exit 1;
	fi

	if [ $MODEL == 'A9000' ]	
	then
		systemctl enable heartbeat
		STATE9=$?
		
    	if [ $STATE9 == 0 ]
    	then
		echo "SUCCESS: Adding services" >> $LOGFILE
    	else
        	echo "FAILURE: Adding services" >> $LOGFILE
        	exit 1;
    	fi
	fi
	
	# deleting list of services not used from systemctl
	systemctl disable mdmonitor >/dev/null 2>&1
	systemctl disable haldaemon >/dev/null 2>&1
	systemctl disable atd >/dev/null 2>&1
	systemctl disable anacron >/dev/null 2>&1
	systemctl disable acpid >/dev/null 2>&1
	systemctl disable messagebus >/dev/null 2>&1
	systemctl disable yum-updatesd >/dev/null 2>&1
	systemctl disable xinetd >/dev/null 2>&1
	systemctl disable ip6tables >/dev/null 2>&1
	systemctl disable bluetooth >/dev/null 2>&1
	systemctl disable cups >/dev/null 2>&1
	systemctl disable cpuspeed >/dev/null 2>&1
	systemctl disable lvm2-monitor >/dev/null 2>&1
	systemctl disable gpm >/dev/null 2>&1
	systemctl disable hidd >/dev/null 2>&1
	systemctl disable pcscd >/dev/null 2>&1
	systemctl disable mcstrans >/dev/null 2>&1
	systemctl disable nfslock >/dev/null 2>&1
	systemctl disable kudzu >/dev/null 2>&1
	systemctl disable netconsole >/dev/null 2>&1
	systemctl disable restorecond >/dev/null 2>&1
	systemctl disable smartd >/dev/null 2>&1
	systemctl disable readahead_early >/dev/null 2>&1
	systemctl disable readahead_later >/dev/null 2>&1
	systemctl disable avahi-daemon >/dev/null 2>&1
	systemctl disable avahi-dnsconfd >/dev/null 2>&1
} 

#
# Permitting serial port 0
#
PermitSerial0 () {
	# note:  MUST COME AFTER auto harden script
	echo "ttyS0" >> /etc/securetty
	if [ $? == 0 ]; then
	    echo "SUCCESS: Permitting serial port 0" >>$LOGFILE;
	else
	    echo "FAILURE: Permitting serial port 0" >>$LOGFILE;
	    exit 1;
	fi
}

#
# CleanUP the intallation script and reboot
#
CleanFirstInstallation () {
	rm -rf $DATA_PATH
	rm -rf /tmp/firmware
	rm -rf /root/drbdyes.txt
	echo "-----> Skipped firstboot RPM removal here, as it is not required <----- " >>$LOGFILE;
}

InstallPackages $MODEL

CreateUser
UpdateYumConf
HardenVPMG
#DisableKudzu
SetDefaultPass
CopyKeys
echo "-----> Skipped CopyConfFiles <----- " >>$LOGFILE;
#CopyConfFiles $MODEL
echo "-----> Skipped CreateFailLog <----- " >>$LOGFILE;
#CreateFailLog
echo "-----> Skipped EnableSwap as we have already created seperate swap partition <----- " >>$LOGFILE;
#EnableSwap $MODEL
ModUlimit
VPMGBuidInstall
UpdateKSVersion $MODEL
CopyControllerFirmware
VPMGRecovery
OptimizeServices $MODEL
PermitSerial0
CleanFirstInstallation
